package cn.com.nes.site.service.logs.impl;

import cn.com.nes.mybatis.agent.logs.entity.ImportLogs;
import cn.com.nes.mybatis.agent.logs.mapper.ImportLogsMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.em.project.ImportLogsTypeEnum;
import cn.com.nes.site.entity.vo.project.ImportGovernmentalCustomerArchiveVO;
import cn.com.nes.site.entity.vo.workOrder.BatchDistributeWorkOrderInstallVO;
import cn.com.nes.site.service.logs.ImportLogsService;
import cn.com.nes.site.service.logs.bo.ImportMainLogPageBO;
import cn.com.nes.site.service.logs.dto.ImportMainLogDTO;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ImportLogsServiceImpl extends ServiceImpl<ImportLogsMapper, ImportLogs> implements ImportLogsService {

    @Resource
    ImportLogsMapper importLogsMapper;

    @Override
    @Async
    public void insertWorkOrderData(BatchDistributeWorkOrderInstallVO vo, List<String> customerIdList) {
        ImportLogs importLogs = new ImportLogs();
        importLogs.setFailNumber(0);
        importLogs.setProjectId(vo.getProjectId());
        Date date = new Date(vo.getOperateTimeStamp());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        importLogs.setDrpc(formattedDate);
        importLogs.setXzqhdm(vo.getXzqhdm());
        importLogs.setTotalNumber(customerIdList.size());
        importLogs.setType("02");
        importLogsMapper.insert(importLogs);
    }

    @Override
    public void insertCustomerInfoData(ImportGovernmentalCustomerArchiveVO childVo, Integer number) {
        // 如果本导入批次已保存了导入日志主表数据，就不需要再保存了
        LambdaQueryWrapper<ImportLogs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportLogs::getProjectId, childVo.getProjectId());
        queryWrapper.eq(ImportLogs::getOperateTimeStamp, String.valueOf(childVo.getOperateTimeStamp()));
        queryWrapper.eq(ImportLogs::getType, ImportLogsTypeEnum.PROJECT_IMPORT.getKey());
        Integer count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return;
        }

        ImportLogs importLogs = new ImportLogs();
        importLogs.setFailNumber(0);
        importLogs.setProjectId(childVo.getProjectId());
        Date date = new Date(childVo.getOperateTimeStamp());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDate = sdf.format(date);
        importLogs.setDrpc(String.valueOf(childVo.getOperateTimeStamp()));
        importLogs.setXzqhdm(childVo.getUserInfo().getAreacode());
        importLogs.setTotalNumber(number);
        importLogs.setType(ImportLogsTypeEnum.PROJECT_IMPORT.getKey());
        importLogs.setProjectName(childVo.getProjectName());
        importLogs.setOperateUser(childVo.getUserInfo().getNickName());
        importLogsMapper.insert(importLogs);
    }

    @Override
    public void updateCustomerInfoData(String projectId, String operateTimeStamp) {
        importLogsMapper.updateCustomerInfoData(projectId, operateTimeStamp);
    }

    @Override
    public PageData queryLogsData(ImportLogs importLogs) {
        Page<ImportLogs> page = new Page<>(importLogs.getPageid(), importLogs.getPagesize());
        List<ImportLogs> list = importLogsMapper.queryLogsData(page, importLogs);
        return PageData.ok(page, list);
    }

    @Override
    public Page<ImportMainLogDTO> importMainLogPage(ImportMainLogPageBO bo) throws Exception {
        Page<ImportMainLogDTO> page = new Page<>(bo.getPageid(), bo.getPagesize());
        List<ImportMainLogDTO> list = this.baseMapper.importMainLogPage(page, bo);
        page.setRecords(list);
        return page;
    }

    @Override
    public void increaseFailCount(ImportGovernmentalCustomerArchiveVO childVo, int failCount) {
        LambdaQueryWrapper<ImportLogs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportLogs::getProjectId, childVo.getProjectId());
        queryWrapper.eq(ImportLogs::getOperateTimeStamp, String.valueOf(childVo.getOperateTimeStamp()));
        queryWrapper.eq(ImportLogs::getType, ImportLogsTypeEnum.PROJECT_IMPORT.getKey());

        List<ImportLogs> importLogs = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(importLogs) || ObjectUtil.isNull(importLogs.get(0))) {
            log.error("本批次导入未记录导入日志,增加导入错误条数失败!");
            return;
        }

        ImportLogs importLog = importLogs.get(0);
        importLog.setFailNumber(ObjectUtil.isNull(importLog.getFailNumber()) ? failCount : importLog.getFailNumber() + failCount);
        Integer rows = this.baseMapper.updateById(importLog);
        if (rows <= 0) {
            log.error("增加导入错误条数失败!");
        }
    }

}
